package cn.iocoder.yudao.module.contract.controller.admin.contractType;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.contract.controller.admin.contractType.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.contractType.ContractTypeDO;
import cn.iocoder.yudao.module.contract.service.contractType.ContractTypeService;

@Tag(name = "管理后台 - 合同类型")
@RestController
@RequestMapping("/contract/type")
@Validated
public class ContractTypeController {

    @Resource
    private ContractTypeService typeService;

    @PostMapping("/create")
    @Operation(summary = "创建合同类型")
    @PreAuthorize("@ss.hasPermission('contract:type:create')")
    public CommonResult<Long> createType(@Valid @RequestBody ContractTypeSaveReqVO createReqVO) {
        return success(typeService.createType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合同类型")
    @PreAuthorize("@ss.hasPermission('contract:type:update')")
    public CommonResult<Boolean> updateType(@Valid @RequestBody ContractTypeSaveReqVO updateReqVO) {
        typeService.updateType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('contract:type:delete')")
    public CommonResult<Boolean> deleteType(@RequestParam("id") Long id) {
        typeService.deleteType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('contract:type:query')")
    public CommonResult<ContractTypeRespVO> getType(@RequestParam("id") Long id) {
        ContractTypeDO type = typeService.getType(id);
        return success(BeanUtils.toBean(type, ContractTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同类型分页")
    @PreAuthorize("@ss.hasPermission('contract:type:query')")
    public CommonResult<PageResult<ContractTypeRespVO>> getTypePage(@Valid ContractTypePageReqVO pageReqVO) {
        PageResult<ContractTypeDO> pageResult = typeService.getTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContractTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合同类型 Excel")
    @PreAuthorize("@ss.hasPermission('contract:type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTypeExcel(@Valid ContractTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContractTypeDO> list = typeService.getTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合同类型.xls", "数据", ContractTypeRespVO.class,
                        BeanUtils.toBean(list, ContractTypeRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得合同类型分页")
    public CommonResult<List<ContractTypeRespVO>> getTypeList(@Valid ContractTypeDO typeDO) {
        List<ContractTypeDO> pageResult = typeService.getTypeList(typeDO);
        return success(BeanUtils.toBean(pageResult, ContractTypeRespVO.class));
    }


}